<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/26
 * Time: 上午10:43
 */
class User_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
        $this->load->helper("common");
        $this->load->model("customer_model");
        
    }

    // 设置用户的默认地址
    public function change_user_default_address($customer_id, $shop_id)
    {
        $this->db->trans_begin();
        $bool1 = $this->db->where(array("customer_id" => $customer_id))->update('app_address', ['is_default' => 0]);
        $bool2 = $this->db->where(array("customer_id" => $customer_id, "shop_id" => $shop_id))->update('app_address', ['is_default' => 1]);

        if ($bool1 && $bool2) {
            $this->db->trans_commit();
            return true;
        } else {
            $this->db->trans_rollback();
            return false;
        }
    }

    // 设置用户的默认地址
    public function change_user_default_address_byid($customer_id, $id)
    {
        $this->db->trans_begin();
        $bool1 = $this->db->where(array("customer_id" => $customer_id))->update('app_address', ['is_default' => 0]);
        $bool2 = $this->db->where(array("customer_id" => $customer_id, "id" => $id))->update('app_address', ['is_default' => 1]);
        if ($bool1 && $bool2) {
            $this->db->trans_commit();
            return true;
        } else {
            $this->db->trans_rollback();
            return false;
        }
    }

    // 新增收获地址
    public function add_address($data)
    {
        if (!isset($data['customer_id']) || !$data['customer_id']) {
            return false;
        }
        $insert_arr['customer_id'] = $data['customer_id'];
        $insert_arr['name'] = isset($data['name']) ? $data['name'] : '';
        $insert_arr['province'] = isset($data['province']) ? $data['province'] : '';
        $insert_arr['city'] = isset($data['city']) ? $data['city'] : '';
        $insert_arr['district'] = isset($data['district']) ? $data['district'] : '';
        $insert_arr['address'] = isset($data['address']) ? $data['address'] : '';
        $insert_arr['postcode'] = isset($data['postcode']) ? $data['postcode'] : '';
        $insert_arr['phone'] = isset($data['phone']) ? $data['phone'] : '';
        $insert_arr['tel'] = isset($data['tel']) ? $data['tel'] : '';
        $insert_arr['region_id'] = isset($data['region_id']) ? $data['region_id'] : '';
        $insert_arr['shop_id'] = isset($data['shop_id']) ? $data['shop_id'] : '';
        $insert_arr['is_default'] = isset($data['is_default']) ? $data['is_default'] : 0;
        $this->db->insert("app_address", $insert_arr);
        if ($re = $this->db->insert_id()) {
            return $re;
        } else {
            return false;
        }
    }


    // 设置用户的默认发票信息
    public function change_user_default_invoice($customer_id, $shop_id)
    {
        $this->db->trans_begin();
        $bool1 = $this->db->where(array("customer_id" => $customer_id))->update('app_invoice', ['is_default' => 0]);
        $bool2 = $this->db->where(array("customer_id" => $customer_id, "shop_id" => $shop_id))->update('app_invoice', ['is_default' => 1]);

        if ($bool1 && $bool2) {
            $this->db->trans_commit();
            return true;
        } else {
            $this->db->trans_rollback();
            return false;
        }
    }


    // shop_user_id 转 customer_id
    public function shop_user_id_to_customer_id($shop_user_id)
    {
        $sql = 'select id from app_customer WHERE shop_user_id = ' . $shop_user_id;
        $customer_id = $this->db->query($sql)->row_array()['id'];
        return $customer_id;
    }

    // customer_id 转 shop_user_id
    public function customer_id_to_shop_user_id($customer_id)
    {
        $sql = 'select shop_user_id from app_customer WHERE id = ' . $customer_id;
        $shop_user_id = $this->db->query($sql)->row_array()['shop_user_id'];
        return $shop_user_id;
    }

    //根据用户id获取admin在商城的id
    public function customer_id_to_admin_to_shop_user_id($customer_id)
    {
        $admin_id = $this->customer_model->get_admin_customer_id($customer_id);
        $sql = 'select shop_user_id from app_customer WHERE id = ' . $admin_id;
        $shop_user_id = $this->db->query($sql)->row_array()['shop_user_id'];
        return $shop_user_id;
    }

    //获取parent_id
    public function customer_id_to_parent_id($customer_id)
    {
        $sql = "select parent_id from app_customer WHERE id = " . $customer_id;
        return $this->db->query($sql)->row_array()['parent_id'];
    }

    public function customer_id_to_admin_id($customer_id)
    {
        $sql = "select parent_id from app_customer WHERE id = " . $customer_id;
        $parent_id = $this->db->query($sql)->row_array()['parent_id'];
        $sqls = "select parent_id from app_customer WHERE id = " . $parent_id;
        return $this->db->query($sqls)->row_array()['parent_id'] ? $this->db->query($sqls)->row_array()['parent_id'] : $parent_id;

    }

    //获取identity
    public function customer_id_to_identity($customer_id)
    {
        $sql = "select identity from app_customer WHERE id = " . $customer_id;
        return $this->db->query($sql)->row_array()['identity'];
    }

    // 用户详情 by shop_user_id
    public function user_detail_by_shop_user_id($shop_user_id)
    {
        $sql = 'select * from app_customer WHERE shop_user_id = ' . $shop_user_id;
        $customer_info = $this->db->query($sql)->row_array();
        $customer_id = $customer_info['id'];
        $sec_info = '';
        switch ($customer_info['identity']) {
            case '1':
                $sec_info = $this->db->query("select * from app_admin WHERE customer_id = " . $customer_id)->row_array();
                unset($sec_info['id']);
                break;

            case '2':
                $sec_info = $this->db->query("select * from app_manager WHERE customer_id = " . $customer_id)->row_array();
                unset($sec_info['id']);
                break;

            case '3':
                $sec_info = $this->db->query("select * from app_user WHERE customer_id = " . $customer_id)->row_array();
                unset($sec_info['id']);
                break;

        }
        $customer_info = array_merge($sec_info, $customer_info);
        return $customer_info;
    }

    // 查询用户的地址信息
    public function query_user_address_list($customer_id)
    {
        $sql = 'select * from app_address WHERE customer_id = ' . $customer_id;
        $user_address_list = $this->db->query($sql)->result_array();
        return $user_address_list;
    }

    // 查询用户的发票信息
    public function query_user_invoice_list($customer_id)
    {
        $sql = 'select * from app_invoice WHERE customer_id = ' . $customer_id;
        $user_address_list = $this->db->query($sql)->result_array();
        return $user_address_list;
    }

    //通过类型查询用户默认发票信息
    public function query_user_invoice_type_default($customer_id, $type)
    {
        $where = array(
            'invoice_type' => $type,
            'is_default' => 1,
            'customer_id' => $customer_id
        );
        $user_address_list = $this->db->where($where)->from('app_invoice')->get()->row_array();
        return $user_address_list;
    }

    // 比对地址信息
    public function compare_address_info($customer_id, $name, $phone, $province, $city, $district, $address)
    {
        $sql = 'select * from app_address WHERE customer_id = ' . $customer_id;
        $user_address_list = $this->db->query($sql)->result_array();
        foreach ($user_address_list as $user_address_info) {
            $u_name = $user_address_info['name'];
            $u_phone = $user_address_info['phone'];
            $u_province = $user_address_info['province'];
            $u_city = $user_address_info['city'];
            $u_district = $user_address_info['district'];
            $u_address = trim($user_address_info['address']);
            if ($u_name == $name && $u_phone == $phone && $province == $u_province && $u_city == $city && $u_district == $district && $u_address == $address) {
                $res['id'] = $user_address_info['id'];
                $res['shop_id'] = $user_address_info['shop_id'];
                return $res;
            }
        }
        return '';
    }

    // 保存manager_user 信息
    public function save_manager_user($data, $id = '')
    {
        $info['customer_id'] = isset($data['customer_id']) ? $data['customer_id'] : '';
        $info['password'] = isset($data['password']) ? $data['password'] : '';
        $info['total_quota'] = isset($data['total_quota']) ? $data['total_quota'] : '';
        $info['single_quota'] = isset($data['single_quota']) ? $data['single_quota'] : '';
        $info['activation'] = isset($data['activation']) ? $data['activation'] : '';
        if ($id) {
            $bool = $this->db->where(array("customer_id" => $id))->update('app_manager_user', $info);
        } else {
            $bool = $this->db->insert("app_manager_user", $info);
        }
        return $bool;

    }

    // 保存manager/user月预算信息（app_budget）
    public function save_manager_user_budget($data, $id = '', $month = '')
    {
        $info['customer_id'] = isset($data['customer_id']) ? $data['customer_id'] : '';
        $info['budget'] = isset($data['budget']) ? $data['budget'] : 0;
        $info['used'] = isset($data['used']) ? $data['used'] : 0;
        $info['month'] = isset($data['month']) ? $data['month'] : '';
        if ($id && $month) {
            $bool = $this->db->where(array("customer_id" => $id, 'month' => $month))->update('app_budget', $info);
        } else {
            $bool = $this->db->insert("app_budget", $info);
        }
        return $bool;

    }


}